<!DOCTYPE html>

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>cup.util.conf &#8212; cup 3.2.33 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../../_static/alabaster.css" />
    <script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
    <script src="../../../_static/doctools.js"></script>
    <script src="../../../_static/sphinx_highlight.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
   
  <link rel="stylesheet" href="../../../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <h1>Source code for cup.util.conf</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*</span>
<span class="c1"># Copyright: [CUP] - See LICENSE for details.</span>
<span class="c1"># Authors: Liu.Jia@baidu, Guannan Ma (@mythmgn),</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">:description:</span>
<span class="sd">    Complex and constructive conf support</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">copy</span>
<span class="kn">import</span> <span class="nn">json</span>
<span class="kn">import</span> <span class="nn">codecs</span>
<span class="kn">import</span> <span class="nn">shutil</span>
<span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">import</span> <span class="nn">functools</span>
<span class="kn">from</span> <span class="nn">xml.dom</span> <span class="kn">import</span> <span class="n">minidom</span>

<span class="kn">import</span> <span class="nn">cup</span>
<span class="kn">from</span> <span class="nn">cup</span> <span class="kn">import</span> <span class="n">platforms</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span>
    <span class="s1">&#39;Configure2Dict&#39;</span><span class="p">,</span> <span class="s1">&#39;Dict2Configure&#39;</span><span class="p">,</span>
    <span class="s1">&#39;HdfsXmlConf&#39;</span>
<span class="p">]</span>


<span class="c1"># pylint: disable=R0913</span>
<span class="c1"># to be compatible with py3 open</span>
<span class="k">def</span> <span class="nf">_open_codecs</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;r&#39;</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;open_codecs for py2 in order to support encoding&quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">codecs</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">filename</span><span class="o">=</span><span class="n">filename</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="n">mode</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="n">encoding</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">CConf</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Depreciated class. Please do not use it. Use python configparser instead.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">revert_version</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">path</span> <span class="o">=</span> <span class="n">path</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">file_abspath</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">path</span> <span class="o">+</span> <span class="s1">&#39;/&#39;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">exclude</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;# &#39;</span><span class="p">,</span> <span class="s1">&#39;[&#39;</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sep</span> <span class="o">=</span> <span class="s1">&#39;:&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">bakfile</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">path</span> <span class="o">+</span> <span class="s1">&#39;/&#39;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">+</span> <span class="s1">&#39;.bak.&#39;</span> <span class="o">+</span> <span class="n">revert_version</span>

    <span class="k">def</span> <span class="fm">__del__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bakfile</span><span class="p">):</span>
            <span class="n">os</span><span class="o">.</span><span class="n">unlink</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bakfile</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_backup</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">new_bakfile</span><span class="p">):</span>
        <span class="n">shutil</span><span class="o">.</span><span class="n">copyfile</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">file_abspath</span><span class="p">,</span> <span class="n">new_bakfile</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">file_abspath</span><span class="p">)</span> <span class="k">as</span> <span class="n">src</span><span class="p">:</span>
            <span class="n">value</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
            <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">src</span><span class="o">.</span><span class="n">readlines</span><span class="p">():</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">line</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">line</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">exclude</span><span class="p">:</span>
                    <span class="n">spstrs</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;:&#39;</span><span class="p">)</span>
                    <span class="n">k</span> <span class="o">=</span> <span class="n">spstrs</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
                    <span class="k">if</span> <span class="n">k</span> <span class="o">==</span> <span class="n">key</span><span class="p">:</span>
                        <span class="n">value</span> <span class="o">=</span> <span class="n">spstrs</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">value</span>

    <span class="k">def</span> <span class="fm">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">            This function should not be used</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="mi">0</span>

    <span class="k">def</span> <span class="nf">update</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">kvs</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        update conf with a dict.</span>

<span class="sd">        dict = {&#39;key&#39; : &#39;value&#39;, &#39;key1&#39;: &#39;value&#39;}</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_backup</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bakfile</span><span class="p">)</span>
        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bakfile</span><span class="p">)</span> <span class="k">as</span> <span class="n">src</span><span class="p">:</span>
            <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">file_abspath</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">trg</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">src</span><span class="o">.</span><span class="n">readlines</span><span class="p">():</span>
                    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">line</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">line</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">exclude</span><span class="p">:</span>
                        <span class="n">splist</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">splistlit</span><span class="p">(</span><span class="s1">&#39;:&#39;</span><span class="p">)</span>
                        <span class="n">k</span> <span class="o">=</span> <span class="n">splist</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
                        <span class="k">if</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">kvs</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
                            <span class="n">line</span> <span class="o">=</span> <span class="n">k</span> <span class="o">+</span> <span class="s1">&#39; : &#39;</span> <span class="o">+</span> <span class="n">kvs</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span>
                    <span class="n">trg</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">revert</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        revert the conf</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">os</span><span class="o">.</span><span class="n">rename</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">bakfile</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">file_abspath</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">write_kv_into_conf</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">kvkvs</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        将key-value写进conf</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">file_abspath</span><span class="p">,</span> <span class="s1">&#39;w+&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fhandle</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">kvkvs</span><span class="o">.</span><span class="n">keys</span><span class="p">:</span>
                <span class="n">fhandle</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">:</span><span class="si">%s</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">kvkvs</span><span class="p">[</span><span class="n">i</span><span class="p">]))</span>


<span class="k">class</span> <span class="nc">CConfModer</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    deprecated. Recommand using Configure2Dict / Dict2Configure</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">toolpath</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">toolpath</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">IOError</span><span class="p">(</span>
                <span class="s1">&#39;File not found - The cfmod tool cannot be found: </span><span class="si">%s</span><span class="s1">&#39;</span>
                <span class="o">%</span> <span class="n">toolpath</span>
            <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_modtool</span> <span class="o">=</span> <span class="n">toolpath</span>

    <span class="k">def</span> <span class="nf">updatekv</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">confpath</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">val</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        update key with value</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">cmd</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> -c </span><span class="si">%s</span><span class="s2"> -u </span><span class="si">%s</span><span class="s2">:</span><span class="si">%s</span><span class="s2"> &quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_modtool</span><span class="p">,</span> <span class="n">confpath</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">val</span><span class="p">)</span>
        <span class="n">try_times</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
            <span class="n">ret</span> <span class="o">=</span> <span class="n">cup</span><span class="o">.</span><span class="n">shell</span><span class="o">.</span><span class="n">ShellExec</span><span class="p">()</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="mi">120</span><span class="p">)</span>
            <span class="k">if</span><span class="p">(</span>
                <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;returncode&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span>
                <span class="ow">or</span> <span class="ow">not</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;returncode&#39;</span><span class="p">]</span>
                <span class="ow">or</span> <span class="n">try_times</span> <span class="o">&gt;</span> <span class="mi">1</span>
            <span class="p">):</span>
                <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;gbk&#39;</span><span class="p">)</span>
                <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
                <span class="c1"># print ret[&#39;stdout&#39;]</span>
                <span class="k">break</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">try_times</span> <span class="o">+=</span> <span class="mi">1</span>
                <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;err:updatekv&#39;</span><span class="p">)</span>
                <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">updatekvlist</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">confpath</span><span class="p">,</span> <span class="n">kvlist</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        update a list of key/value</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">strcmd</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="k">for</span> <span class="n">key_value</span> <span class="ow">in</span> <span class="n">kvlist</span><span class="p">:</span>
            <span class="n">strcmd</span> <span class="o">+=</span> <span class="s1">&#39; -u </span><span class="si">%s</span><span class="s1">:</span><span class="si">%s</span><span class="s1"> &#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">key_value</span><span class="p">[</span><span class="s1">&#39;key&#39;</span><span class="p">],</span> <span class="n">key_value</span><span class="p">[</span><span class="s1">&#39;value&#39;</span><span class="p">])</span>
        <span class="n">cmd</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">  -c </span><span class="si">%s</span><span class="s2"> </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_modtool</span><span class="p">,</span> <span class="n">confpath</span><span class="p">,</span> <span class="n">strcmd</span><span class="p">)</span>
        <span class="n">try_times</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
            <span class="n">ret</span> <span class="o">=</span> <span class="n">cup</span><span class="o">.</span><span class="n">shell</span><span class="o">.</span><span class="n">ShellExec</span><span class="p">()</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="mi">120</span><span class="p">)</span>
            <span class="k">if</span><span class="p">(</span>
                <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;returncode&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span>
                <span class="ow">or</span> <span class="ow">not</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;returncode&#39;</span><span class="p">]</span>
                <span class="ow">or</span> <span class="n">try_times</span> <span class="o">&gt;</span> <span class="mi">1</span>
            <span class="p">):</span>
                <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;gbk&#39;</span><span class="p">)</span>
                <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
                <span class="nb">print</span><span class="p">(</span><span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">])</span>
                <span class="k">break</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">try_times</span> <span class="o">+=</span> <span class="mi">1</span>
                <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;err:updatekvlist&#39;</span><span class="p">)</span>
                <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">addkv</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">confpath</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">val</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        add key value into a conf</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">cmd</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> -c </span><span class="si">%s</span><span class="s2"> -i </span><span class="si">%s</span><span class="s2">:</span><span class="si">%s</span><span class="s2"> &amp;&gt;/dev/null&quot;</span> <span class="o">%</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_modtool</span><span class="p">,</span> <span class="n">confpath</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">val</span>
        <span class="p">)</span>
        <span class="n">try_times</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
            <span class="n">ret</span> <span class="o">=</span> <span class="n">cup</span><span class="o">.</span><span class="n">shell</span><span class="o">.</span><span class="n">ShellExec</span><span class="p">()</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="mi">120</span><span class="p">)</span>
            <span class="k">if</span><span class="p">(</span>
                <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;returncode&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span>
                <span class="ow">or</span> <span class="ow">not</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;returncode&#39;</span><span class="p">]</span>
                <span class="ow">or</span> <span class="n">try_times</span> <span class="o">&gt;</span> <span class="mi">1</span>
            <span class="p">):</span>
                <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;gbk&#39;</span><span class="p">)</span>
                <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
                <span class="nb">print</span><span class="p">(</span><span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">])</span>
                <span class="k">break</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">try_times</span> <span class="o">+=</span> <span class="mi">1</span>
                <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;err:addkv&#39;</span><span class="p">)</span>
                <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

            <span class="k">if</span><span class="p">(</span><span class="n">ret</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">try_times</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">):</span>
                <span class="nb">print</span><span class="p">(</span><span class="n">cmd</span><span class="p">)</span>
                <span class="k">break</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
                <span class="n">try_times</span> <span class="o">+=</span> <span class="mi">1</span>

    <span class="k">def</span> <span class="nf">delkv</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">confpath</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        del a key from a conf file</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">cmd</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> -c </span><span class="si">%s</span><span class="s2"> -d </span><span class="si">%s</span><span class="s2"> &quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_modtool</span><span class="p">,</span> <span class="n">confpath</span><span class="p">,</span> <span class="n">key</span><span class="p">)</span>
        <span class="n">try_times</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
            <span class="n">ret</span> <span class="o">=</span> <span class="n">cup</span><span class="o">.</span><span class="n">shell</span><span class="o">.</span><span class="n">ShellExec</span><span class="p">()</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="mi">120</span><span class="p">)</span>
            <span class="k">if</span><span class="p">(</span>
                <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;returncode&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span>
                <span class="ow">or</span> <span class="ow">not</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;returncode&#39;</span><span class="p">]</span>
                <span class="ow">or</span> <span class="n">try_times</span> <span class="o">&gt;</span> <span class="mi">1</span>
            <span class="p">):</span>
                <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;gbk&#39;</span><span class="p">)</span>
                <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
                <span class="nb">print</span><span class="p">(</span><span class="n">ret</span><span class="p">[</span><span class="s1">&#39;stdout&#39;</span><span class="p">])</span>
                <span class="k">break</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">try_times</span> <span class="o">+=</span> <span class="mi">1</span>
                <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;err:delkv&#39;</span><span class="p">)</span>
                <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

            <span class="k">if</span><span class="p">(</span><span class="n">ret</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">try_times</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">):</span>
                <span class="nb">print</span><span class="p">(</span><span class="n">cmd</span><span class="p">)</span>
                <span class="k">break</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
                <span class="n">try_times</span> <span class="o">+=</span> <span class="mi">1</span>


<span class="k">class</span> <span class="nc">ArrayFormatError</span><span class="p">(</span><span class="n">cup</span><span class="o">.</span><span class="n">err</span><span class="o">.</span><span class="n">BaseCupException</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    array format error for Configure2Dict</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">errmsg</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">errmsg</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">LineFormatError</span><span class="p">(</span><span class="n">cup</span><span class="o">.</span><span class="n">err</span><span class="o">.</span><span class="n">BaseCupException</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Line error class</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">errmsg</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">errmsg</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">KeyFormatError</span><span class="p">(</span><span class="n">cup</span><span class="o">.</span><span class="n">err</span><span class="o">.</span><span class="n">BaseCupException</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Key error class</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">errmsg</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">errmsg</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">ValueFormatError</span><span class="p">(</span><span class="n">cup</span><span class="o">.</span><span class="n">err</span><span class="o">.</span><span class="n">BaseCupException</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    value error class</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">errmsg</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">errmsg</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">UnknowConfError</span><span class="p">(</span><span class="n">cup</span><span class="o">.</span><span class="n">err</span><span class="o">.</span><span class="n">BaseCupException</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    unkown error class</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">errmsg</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">errmsg</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">ConfDictSetItemError</span><span class="p">(</span><span class="n">cup</span><span class="o">.</span><span class="n">err</span><span class="o">.</span><span class="n">BaseCupException</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    ConfDict Error</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">errmsg</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">errmsg</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">ConfListSetItemError</span><span class="p">(</span><span class="n">cup</span><span class="o">.</span><span class="n">err</span><span class="o">.</span><span class="n">BaseCupException</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    ConfList Error</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">errmsg</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">errmsg</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">ConfList</span><span class="p">(</span><span class="nb">list</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Conf list attributes.</span>

<span class="sd">    e.g.</span>

<span class="sd">    @disk: /home/disk1</span>
<span class="sd">    @disk: /home/disk2</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_ind</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_comments</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">append_ex</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">,</span> <span class="n">comments</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        append a item with conf comments</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">assert</span> <span class="nb">type</span><span class="p">(</span><span class="n">comments</span><span class="p">)</span> <span class="o">==</span> <span class="nb">list</span><span class="p">,</span> <span class="s1">&#39;comments should be a list&#39;</span>
        <span class="nb">super</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_ind</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_comments</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">comments</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_ex</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ind</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get conf list item with its comments</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="fm">__getitem__</span><span class="p">(</span><span class="n">ind</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">_comments</span><span class="p">[</span><span class="n">ind</span><span class="p">])</span>
        <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="fm">__getitem__</span><span class="p">(</span><span class="n">ind</span><span class="p">),</span> <span class="p">[])</span>

    <span class="k">def</span> <span class="fm">__delitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">index</span><span class="p">):</span>
        <span class="nb">list</span><span class="o">.</span><span class="fm">__delitem__</span><span class="p">(</span><span class="n">index</span><span class="p">)</span>
        <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_comments</span><span class="p">[</span><span class="n">index</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">append</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;append item&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">append_ex</span><span class="p">(</span><span class="n">item</span><span class="p">,</span> <span class="p">[])</span>

    <span class="k">def</span> <span class="nf">insert</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ind</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;plz do not use this function&quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="n">ConfDictSetItemError</span><span class="p">(</span>
            <span class="s1">&#39;Do not support &quot;insert&quot;. Use &quot;append&quot; instead&#39;</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">extend</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">seqs</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;plz do not use extend&quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="n">ConfDictSetItemError</span><span class="p">(</span>
            <span class="s1">&#39;Do not support &quot;extend&quot;. Use &quot;append&quot; instead&#39;</span>
        <span class="p">)</span>


<span class="k">class</span> <span class="nc">ConfDict</span><span class="p">(</span><span class="nb">dict</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    ConfDict that Configure2Dict and Dict2Configure can use.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_index</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_extra_dict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_tail</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_reverse_ind</span> <span class="o">=</span> <span class="o">-</span><span class="mi">99999999</span>

    <span class="k">def</span> <span class="fm">__delitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">ConfDict</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__delitem__</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
        <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_extra_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">set_ex</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">comments</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        In addtion to dict[&#39;key&#39;] = value, set_ex also set comments along with</span>
<span class="sd">        the key.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">ConfDict</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__setitem__</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_extra_dict</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">list</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_extra_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_index</span><span class="p">,</span> <span class="n">comments</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_index</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_extra_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_reverse_ind</span><span class="p">,</span> <span class="n">comments</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_reverse_ind</span> <span class="o">+=</span> <span class="mi">1</span>

    <span class="k">def</span> <span class="nf">get_ex</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get (value, comments) with key, comments is a list</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
        <span class="n">comments</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_extra_dict</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">comments</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">comments</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">comments</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__setitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__setitem__</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_extra_dict</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">list</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_extra_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_index</span><span class="p">,</span> <span class="p">[])</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_index</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_extra_dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_reverse_ind</span><span class="p">,</span> <span class="p">[])</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_reverse_ind</span> <span class="o">+=</span> <span class="mi">1</span>

    <span class="k">def</span> <span class="nf">_compare_keys</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">keyx</span><span class="p">,</span> <span class="n">keyy</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;compare keys&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_extra_dict</span><span class="p">[</span><span class="n">keyx</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_extra_dict</span><span class="p">[</span><span class="n">keyy</span><span class="p">][</span><span class="mi">0</span><span class="p">]:</span>
            <span class="k">return</span> <span class="mi">0</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">_extra_dict</span><span class="p">[</span><span class="n">keyx</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_extra_dict</span><span class="p">[</span><span class="n">keyy</span><span class="p">][</span><span class="mi">0</span><span class="p">]:</span>
            <span class="k">return</span> <span class="o">-</span><span class="mi">1</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="mi">1</span>

    <span class="k">def</span> <span class="nf">get_ordered_keys</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get keys in order</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">keys</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">keys</span><span class="p">(),</span> <span class="n">key</span><span class="o">=</span><span class="n">functools</span><span class="o">.</span><span class="n">cmp_to_key</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_compare_keys</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">keys</span>


<span class="c1">#  @brief translate configure(public/configure) conf to dict</span>
<div class="viewcode-block" id="Configure2Dict"><a class="viewcode-back" href="../../../cup.util.html#cup.util.conf.Configure2Dict">[docs]</a><span class="k">class</span> <span class="nc">Configure2Dict</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>  <span class="c1"># pylint: disable=R0903</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Configure2Dict support conf features below:</span>

<span class="sd">    1. comments</span>

<span class="sd">        As we support access/modify comments in a conf file, you should obey</span>
<span class="sd">        rules below:</span>

<span class="sd">        Comment closely above the object you want to comment.</span>
<span class="sd">        Do NOT comment after the line.</span>

<span class="sd">        Otherwise, you might get/set a wrong comment above the object.</span>

<span class="sd">    2. sections</span>

<span class="sd">        2.1 global section</span>

<span class="sd">            - if key:value is not under any [section],</span>
<span class="sd">              it is under the global layerby default</span>
<span class="sd">            - global section is the 0th layer section</span>

<span class="sd">            e.g.</span>
<span class="sd">            ::</span>

<span class="sd">                # test.conf</span>
<span class="sd">                global-key: value</span>
<span class="sd">                global-key1: value1</span>

<span class="sd">        2.2 child section</span>

<span class="sd">            - [section1] means a child section under Global. And it&#39;s the</span>
<span class="sd">              1st layer section</span>
<span class="sd">            - [.section2] means a child section under the nearest section</span>
<span class="sd">              above. It&#39;s the 2nd layer section.</span>
<span class="sd">            - [..section3] means a child section under the nearest section</span>
<span class="sd">              above. And the prefix .. means it is the 3rd layer section</span>

<span class="sd">            e.g.: test.conf:</span>
<span class="sd">            ::</span>

<span class="sd">                global-key: value</span>
<span class="sd">                [section]</span>
<span class="sd">                    host:  abc.com</span>
<span class="sd">                    port:  8080</span>
<span class="sd">                    [.section_child]</span>
<span class="sd">                        child_key: child_value</span>
<span class="sd">                        [..section_child_child]</span>
<span class="sd">                            control: ssh</span>
<span class="sd">                            [...section_child_child_child]</span>
<span class="sd">                                wow_key:  wow_value</span>

<span class="sd">        2.3 section access method</span>

<span class="sd">            get_dict method will convert conf into a ConfDict which is derived</span>
<span class="sd">            from python dict.</span>

<span class="sd">            - Access the section with confdict[&#39;section&#39;][&#39;section-child&#39;].</span>
<span class="sd">            - Access the section with confdict.get_ex(&#39;section&#39;) with (value,</span>
<span class="sd">              comments)</span>


<span class="sd">    3. key:value and key:value array</span>

<span class="sd">        3.1 key:value</span>

<span class="sd">            key:value can be set under Global section which is closely after the</span>
<span class="sd">            1st line with no [section] above.</span>

<span class="sd">            key:value can also be set under sections.</span>
<span class="sd">            ::</span>

<span class="sd">                # test.conf</span>
<span class="sd">                key1: value1</span>
<span class="sd">                [section]</span>
<span class="sd">                    key_section: value_in_section</span>
<span class="sd">                    [.seciton]</span>
<span class="sd">                        key_section_child: value_section_child</span>

<span class="sd">        3.2 key:value arrays</span>

<span class="sd">            key:value arrays can be access with confdict[&#39;section&#39;][&#39;disk&#39;].</span>
<span class="sd">            You will get a ConfList derived from python list.</span>

<span class="sd">        ::</span>

<span class="sd">            # test.conf</span>
<span class="sd">            # Global layer, key:value</span>
<span class="sd">            host: abc.com</span>
<span class="sd">            port: 12345</span>
<span class="sd">            # 1st layer [monitor]</span>
<span class="sd">            @disk: /home/data0</span>
<span class="sd">            @disk: /home/data1</span>
<span class="sd">            [section]</span>
<span class="sd">                @disk: /home/disk/disk1</span>
<span class="sd">                @disk: /home/disk/disk2</span>

<span class="sd">    4. Example</span>

<span class="sd">    ::</span>

<span class="sd">        # test.conf</span>
<span class="sd">        # Global layer, key:value</span>
<span class="sd">        host: abc.com</span>
<span class="sd">        port: 12345</span>
<span class="sd">        # 1st layer [monitor]</span>
<span class="sd">        @disk: /home/data0</span>
<span class="sd">        @disk: /home/data1</span>
<span class="sd">        [section]</span>
<span class="sd">            @disk: /home/disk/disk1</span>
<span class="sd">            @disk: /home/disk/disk2</span>

<span class="sd">        [monitor]</span>
<span class="sd">            timeout: 100</span>
<span class="sd">            regex:  sshd</span>
<span class="sd">            # 2nd layer that belongs to [monitor]</span>
<span class="sd">            [.timeout]</span>
<span class="sd">                # key:value in timeout</span>
<span class="sd">                max: 100</span>
<span class="sd">                # 3rd layer that belongs to [monitor] [timeout]</span>
<span class="sd">                [..handler]</span>
<span class="sd">                    default: exit</span>

<span class="sd">        # codes represent accessing conf and modifying conf</span>
<span class="sd">        from __future__ import print_function</span>
<span class="sd">        from cup.util import conf</span>
<span class="sd">        # 1. read from test.conf</span>
<span class="sd">        confdict = conf.Configure2Dict(&#39;test.conf&#39;, separator=&#39;:&#39;).get_dict()</span>
<span class="sd">        print(confdict[&#39;host&#39;])</span>
<span class="sd">        print(confdict[&#39;port&#39;])</span>
<span class="sd">        print(confdict[&#39;section&#39;][&#39;disk&#39;])</span>
<span class="sd">        print(confdict[&#39;monitor&#39;][&#39;timeout&#39;][&#39;handler&#39;])</span>
<span class="sd">        # 2. Change something and write back to test.conf</span>
<span class="sd">        confdict[&#39;port&#39;] = &#39;10085&#39;</span>
<span class="sd">        confdict[&#39;monitor&#39;][&#39;timeout&#39;][&#39;handler&#39;][&#39;default&#39;] = &#39;stop&#39;</span>
<span class="sd">        confobj = conf.Dict2Configure(confdict, separator=&#39;:&#39;)</span>
<span class="sd">        confobj.write_conf(&#39;./test.conf.new&#39;)</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">configure_file</span><span class="p">,</span> <span class="n">remove_comments</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">separator</span><span class="o">=</span><span class="s1">&#39;:&#39;</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :param configure_file:</span>
<span class="sd">            configure file path</span>
<span class="sd">        :param remove_comments:</span>
<span class="sd">            if you comment after key:value # comment, whether we should</span>
<span class="sd">            remove it when you access the key</span>
<span class="sd">        :raise:</span>
<span class="sd">            IOError configure_file not found</span>
<span class="sd">            cup.util.conf.KeyFormatError Key format error</span>
<span class="sd">            cup.util.conf.ValueFormatError value value</span>
<span class="sd">            cup.util.conf.LineFormatError  line format error</span>
<span class="sd">            cup.util.conf.ArrayFormatError @array format error</span>
<span class="sd">            cup.util.conf.UnknowConfError unknown error</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_file</span> <span class="o">=</span> <span class="n">configure_file</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">configure_file</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">IOError</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> does not exists&#39;</span> <span class="o">%</span> <span class="n">configure_file</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">configure_file</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">IOError</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> is not a file&#39;</span> <span class="o">%</span> <span class="n">configure_file</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_lines</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_dict</span> <span class="o">=</span> <span class="n">ConfDict</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_remove_comments</span> <span class="o">=</span> <span class="n">remove_comments</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_blank_and_comments</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_separator</span> <span class="o">=</span> <span class="n">separator</span>

    <span class="k">def</span> <span class="nf">_strip_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        strip the value</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_remove_comments</span><span class="p">:</span>
            <span class="n">rev</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;#&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">rev</span> <span class="o">=</span> <span class="n">value</span>
        <span class="k">return</span> <span class="n">rev</span>

    <span class="k">def</span> <span class="nf">_handle_key_value_tuple</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">linenum</span><span class="p">,</span> <span class="n">conf_dict_now</span><span class="p">,</span> <span class="n">comments</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        handle (key, value) tuple</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">num</span> <span class="o">=</span> <span class="n">linenum</span>
        <span class="n">line</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lines</span><span class="p">[</span><span class="n">num</span><span class="p">]</span>
        <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="o">=</span> <span class="n">line</span>
        <span class="n">rev_comments</span> <span class="o">=</span> <span class="n">comments</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">key</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;@&#39;</span><span class="p">):</span>
            <span class="n">conf_dict_now</span><span class="o">.</span><span class="n">set_ex</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">rev_comments</span><span class="p">)</span>
            <span class="n">rev_comments</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># @key: value</span>
            <span class="c1"># it&#39;s a conf array.</span>
            <span class="c1"># e.g.</span>
            <span class="c1"># @disk : /home/disk1</span>
            <span class="c1"># @disk : /home/disk2</span>
            <span class="c1"># conf_dict_now[key[1:]] = [value]</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">key</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span> <span class="ow">in</span> <span class="n">conf_dict_now</span><span class="p">:</span>
                <span class="n">conf_array</span> <span class="o">=</span> <span class="n">ConfList</span><span class="p">()</span>
                <span class="n">conf_dict_now</span><span class="o">.</span><span class="n">set_ex</span><span class="p">(</span><span class="n">key</span><span class="p">[</span><span class="mi">1</span><span class="p">:],</span> <span class="n">conf_array</span><span class="p">,</span> <span class="n">rev_comments</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">conf_array</span> <span class="o">=</span> <span class="n">conf_dict_now</span><span class="p">[</span><span class="n">key</span><span class="p">[</span><span class="mi">1</span><span class="p">:]]</span>
            <span class="n">conf_array</span><span class="o">.</span><span class="n">append_ex</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="p">[])</span>
            <span class="n">rev_comments</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">num</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="k">while</span> <span class="n">num</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_lines</span><span class="p">):</span>  <span class="c1"># get all items</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_handle_comments</span><span class="p">(</span><span class="n">rev_comments</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lines</span><span class="p">[</span><span class="n">num</span><span class="p">]):</span>
                    <span class="n">num</span> <span class="o">+=</span> <span class="mi">1</span>
                    <span class="k">continue</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_lines</span><span class="p">[</span><span class="n">num</span><span class="p">])</span> <span class="o">==</span> <span class="nb">tuple</span> <span class="ow">or</span> \
                        <span class="bp">self</span><span class="o">.</span><span class="n">_lines</span><span class="p">[</span><span class="n">num</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">!=</span> <span class="n">key</span><span class="p">:</span>
                    <span class="n">num</span> <span class="o">-=</span> <span class="mi">1</span>
                    <span class="k">break</span>
                <span class="n">conf_dict_now</span><span class="p">[</span><span class="n">key</span><span class="p">[</span><span class="mi">1</span><span class="p">:]]</span><span class="o">.</span><span class="n">append_ex</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_lines</span><span class="p">[</span><span class="n">num</span><span class="p">][</span><span class="mi">1</span><span class="p">],</span> <span class="n">rev_comments</span>
                <span class="p">)</span>
                <span class="n">rev_comments</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="n">num</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">num</span><span class="p">,</span> <span class="n">rev_comments</span><span class="p">)</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_handle_comments</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">comments</span><span class="p">,</span> <span class="n">line</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        handle comments</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">line</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;__comments__&#39;</span><span class="p">:</span>
            <span class="n">comments</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">line</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="kc">False</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_handle_group_keys</span><span class="p">(</span>
        <span class="bp">cls</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">conf_dict_now</span><span class="p">,</span> <span class="n">conf_layer_stack</span><span class="p">,</span> <span class="n">comments</span>
    <span class="p">):</span>
        <span class="k">for</span> <span class="n">groupkey</span> <span class="ow">in</span> <span class="n">key</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">):</span>
            <span class="n">conf_dict_now</span> <span class="o">=</span> <span class="n">conf_layer_stack</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">while</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">conf_dict_now</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
                <span class="n">conf_dict_now</span> <span class="o">=</span> <span class="n">conf_dict_now</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">groupkey</span> <span class="ow">in</span> <span class="n">conf_dict_now</span><span class="p">:</span>
                <span class="n">conf_dict_now</span> <span class="o">=</span> <span class="n">conf_dict_now</span><span class="p">[</span><span class="n">groupkey</span><span class="p">]</span>
                <span class="c1"># push this layer into the stack</span>
                <span class="n">conf_layer_stack</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">conf_dict_now</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">groupkey</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;@&#39;</span><span class="p">:</span>
                    <span class="n">groupkey</span> <span class="o">=</span> <span class="n">groupkey</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
                    <span class="k">if</span> <span class="n">groupkey</span> <span class="ow">in</span> <span class="n">conf_dict_now</span><span class="p">:</span>
                        <span class="n">conf_dict_now</span><span class="p">[</span><span class="n">groupkey</span><span class="p">]</span><span class="o">.</span><span class="n">append_ex</span><span class="p">(</span>
                            <span class="p">{},</span> <span class="n">comments</span>
                        <span class="p">)</span>
                        <span class="n">comments</span> <span class="o">=</span> <span class="p">[]</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">conflist</span> <span class="o">=</span> <span class="n">ConfList</span><span class="p">()</span>
                        <span class="n">conf_dict_now</span><span class="o">.</span><span class="n">set_ex</span><span class="p">(</span>
                            <span class="n">groupkey</span><span class="p">,</span> <span class="n">conflist</span><span class="p">,</span> <span class="n">comments</span>
                        <span class="p">)</span>
                        <span class="n">comments</span> <span class="o">=</span> <span class="p">[]</span>
                        <span class="n">conflist</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ConfDict</span><span class="p">())</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">conf_dict_now</span><span class="o">.</span><span class="n">set_ex</span><span class="p">(</span>
                        <span class="n">groupkey</span><span class="p">,</span> <span class="n">ConfDict</span><span class="p">(),</span> <span class="n">comments</span>
                    <span class="p">)</span>
                    <span class="n">comments</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="n">conf_layer_stack</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">conf_dict_now</span><span class="p">[</span><span class="n">groupkey</span><span class="p">])</span>
        <span class="k">return</span> <span class="n">comments</span>

    <span class="c1"># GLOBAL level 1</span>
    <span class="c1"># [groupA] level 2</span>
    <span class="c1">#     [.@groupB] level 3</span>
    <span class="c1">#         [..@groupC] level 4</span>


    <span class="c1"># pylint: disable=R0912, R0915</span>
<div class="viewcode-block" id="Configure2Dict.get_dict"><a class="viewcode-back" href="../../../cup.util.html#cup.util.conf.Configure2Dict.get_dict">[docs]</a>    <span class="k">def</span> <span class="nf">get_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ignore_error</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="s1">&#39;utf8&#39;</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get conf_dict which you can use to access conf info.</span>

<span class="sd">        :param ignore_error:</span>
<span class="sd">            True, CUP will parse the conf file without catching exceptions</span>
<span class="sd">        :param encoding:</span>
<span class="sd">            utf8 by default, you can change it to your encoding</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">comments</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_get_input_lines</span><span class="p">(</span><span class="n">ignore_error</span><span class="p">,</span> <span class="n">encoding</span><span class="p">)</span>
        <span class="n">conf_layer_stack</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_dict</span><span class="p">]</span>
        <span class="n">num</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">length</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_lines</span><span class="p">)</span>
        <span class="n">last_list_key</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">while</span> <span class="n">num</span> <span class="o">&lt;</span> <span class="n">length</span><span class="p">:</span>
            <span class="n">line</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lines</span><span class="p">[</span><span class="n">num</span><span class="p">]</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_handle_comments</span><span class="p">(</span><span class="n">comments</span><span class="p">,</span> <span class="n">line</span><span class="p">):</span>
                <span class="n">num</span> <span class="o">+=</span> <span class="mi">1</span>
                <span class="k">continue</span>
            <span class="n">conf_dict_now</span> <span class="o">=</span> <span class="n">conf_layer_stack</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>  <span class="c1"># conf_dict_now is current</span>
            <span class="k">while</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">conf_dict_now</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>  <span class="c1"># [], find the working dict</span>
                <span class="n">conf_dict_now</span> <span class="o">=</span> <span class="n">conf_dict_now</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">line</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">):</span>  <span class="c1"># key value</span>
                <span class="n">num</span><span class="p">,</span> <span class="n">comments</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_handle_key_value_tuple</span><span class="p">(</span>
                    <span class="n">num</span><span class="p">,</span> <span class="n">conf_dict_now</span><span class="p">,</span> <span class="n">comments</span>
                <span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">key</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">lstrip</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)</span>
                <span class="n">level</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">line</span><span class="p">)</span> <span class="o">-</span> <span class="nb">len</span><span class="p">(</span><span class="n">key</span><span class="p">)</span> <span class="o">+</span> <span class="mi">2</span>  <span class="c1"># determine the level</span>
                <span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="s1">&#39;GLOBAL&#39;</span><span class="p">:</span> <span class="c1"># GLOBAL is the 1st level</span>
                    <span class="n">level</span> <span class="o">=</span> <span class="mi">1</span>
                    <span class="n">conf_layer_stack</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_dict</span><span class="p">]</span>
                <span class="c1"># [Group1.SubGroup1] sub-key: Value</span>
                <span class="c1"># if sth below level cannot be computed as len(line) - len(key)</span>
                <span class="k">elif</span> <span class="s1">&#39;.&#39;</span> <span class="ow">in</span> <span class="n">key</span><span class="p">:</span>  <span class="c1"># conf_layer_stack back to [self._dict]</span>
                    <span class="n">conf_layer_stack</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_dict</span><span class="p">]</span>
                    <span class="n">comments</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_handle_group_keys</span><span class="p">(</span>
                        <span class="n">key</span><span class="p">,</span> <span class="n">conf_dict_now</span><span class="p">,</span> <span class="n">conf_layer_stack</span><span class="p">,</span> <span class="n">comments</span>
                    <span class="p">)</span>
                <span class="k">elif</span> <span class="n">level</span> <span class="o">&gt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">conf_layer_stack</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">ArrayFormatError</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
                <span class="k">elif</span> <span class="n">level</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">conf_layer_stack</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">:</span>
                    <span class="c1"># new grou for</span>
                    <span class="k">if</span> <span class="n">key</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;@&#39;</span><span class="p">:</span>
                        <span class="n">key</span> <span class="o">=</span> <span class="n">key</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
                        <span class="n">conflist</span> <span class="o">=</span> <span class="n">ConfList</span><span class="p">()</span>
                        <span class="n">conflist</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ConfDict</span><span class="p">())</span>
                        <span class="n">conf_dict_now</span><span class="o">.</span><span class="n">set_ex</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">conflist</span><span class="p">,</span> <span class="p">[])</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">conf_dict_now</span><span class="o">.</span><span class="n">set_ex</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">ConfDict</span><span class="p">(),</span> <span class="n">comments</span><span class="p">)</span>
                        <span class="n">comments</span> <span class="o">=</span> <span class="p">[]</span>
                    <span class="n">conf_layer_stack</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">conf_dict_now</span><span class="p">[</span><span class="n">key</span><span class="p">])</span>
                <span class="k">elif</span> <span class="n">level</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">conf_layer_stack</span><span class="p">):</span>
                    <span class="c1"># -1 means the last item. -2 means the second from the end</span>
                    <span class="n">conf_dict_now</span> <span class="o">=</span> <span class="n">conf_layer_stack</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span>   <span class="c1"># back one</span>
                    <span class="k">while</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">conf_dict_now</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
                        <span class="n">conf_dict_now</span> <span class="o">=</span> <span class="n">conf_dict_now</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
                    <span class="k">if</span> <span class="n">key</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;@&#39;</span><span class="p">:</span>
                        <span class="n">tmpkey</span> <span class="o">=</span> <span class="n">key</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
                        <span class="k">if</span> <span class="n">tmpkey</span> <span class="ow">in</span> <span class="n">conf_dict_now</span><span class="p">:</span>   <span class="c1"># the same group</span>
                            <span class="c1"># pylint: disable=E1101</span>
                            <span class="c1"># conf_dict_now[tmpkey] = ConfDict()</span>
                            <span class="k">if</span> <span class="n">tmpkey</span> <span class="o">!=</span> <span class="n">last_list_key</span><span class="p">:</span>
                                <span class="n">conf_layer_stack</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">conf_dict_now</span><span class="p">[</span><span class="n">tmpkey</span><span class="p">]</span>
                            <span class="n">conf_layer_stack</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">append_ex</span><span class="p">(</span>
                                <span class="n">ConfDict</span><span class="p">(),</span> <span class="n">comments</span>
                            <span class="p">)</span>
                            <span class="n">comments</span> <span class="o">=</span> <span class="p">[]</span>
                        <span class="k">else</span><span class="p">:</span>  <span class="c1"># different group</span>
                            <span class="n">conflist</span> <span class="o">=</span> <span class="n">ConfList</span><span class="p">()</span>
                            <span class="n">conflist</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ConfDict</span><span class="p">())</span>
                            <span class="n">conf_dict_now</span><span class="o">.</span><span class="n">set_ex</span><span class="p">(</span><span class="n">tmpkey</span><span class="p">,</span> <span class="n">conflist</span><span class="p">,</span> <span class="n">comments</span><span class="p">)</span>
                            <span class="c1"># conf_dict_now.set_ex(tmpkey, conflist, [])</span>
                            <span class="n">comments</span> <span class="o">=</span> <span class="p">[]</span>
                            <span class="n">conf_layer_stack</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">conf_dict_now</span><span class="p">[</span><span class="n">tmpkey</span><span class="p">]</span>
                        <span class="n">last_list_key</span> <span class="o">=</span> <span class="n">tmpkey</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">conf_dict_now</span><span class="o">.</span><span class="n">set_ex</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">ConfDict</span><span class="p">(),</span> <span class="n">comments</span><span class="p">)</span>
                        <span class="n">comments</span> <span class="o">=</span> <span class="p">[]</span>
                        <span class="n">conf_layer_stack</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">conf_dict_now</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
                <span class="k">elif</span> <span class="n">level</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">conf_layer_stack</span><span class="p">):</span>
                    <span class="n">conf_dict_now</span> <span class="o">=</span> <span class="n">conf_layer_stack</span><span class="p">[</span><span class="n">level</span> <span class="o">-</span> <span class="mi">2</span><span class="p">]</span>  <span class="c1"># get back</span>
                    <span class="k">while</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">conf_dict_now</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
                        <span class="n">conf_dict_now</span> <span class="o">=</span> <span class="n">conf_dict_now</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
                    <span class="k">if</span> <span class="n">key</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;@&#39;</span><span class="p">:</span>
                        <span class="n">tmpkey</span> <span class="o">=</span> <span class="n">key</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
                        <span class="k">if</span> <span class="n">tmpkey</span> <span class="ow">in</span> <span class="n">conf_dict_now</span><span class="p">:</span>  <span class="c1"># the same group</span>
                            <span class="n">tmpdict</span> <span class="o">=</span> <span class="n">ConfDict</span><span class="p">()</span>
                            <span class="n">conf_layer_stack</span><span class="p">[</span><span class="n">level</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tmpdict</span><span class="p">)</span>
                        <span class="k">else</span><span class="p">:</span>  <span class="c1"># different group</span>
                            <span class="n">conflist</span> <span class="o">=</span> <span class="n">ConfList</span><span class="p">()</span>
                            <span class="n">conflist</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ConfDict</span><span class="p">())</span>
                            <span class="n">conf_dict_now</span><span class="o">.</span><span class="n">set_ex</span><span class="p">(</span><span class="n">tmpkey</span><span class="p">,</span> <span class="n">conflist</span><span class="p">,</span> <span class="p">[])</span>
                            <span class="n">conf_layer_stack</span><span class="p">[</span><span class="n">level</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">conf_dict_now</span><span class="p">[</span><span class="n">tmpkey</span><span class="p">]</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">conf_dict_now</span><span class="o">.</span><span class="n">set_ex</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">ConfDict</span><span class="p">(),</span> <span class="n">comments</span><span class="p">)</span>
                        <span class="n">comments</span> <span class="o">=</span> <span class="p">[]</span>
                        <span class="n">conf_layer_stack</span><span class="p">[</span><span class="n">level</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">conf_dict_now</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
                    <span class="n">conf_layer_stack</span> <span class="o">=</span> <span class="n">conf_layer_stack</span><span class="p">[:</span><span class="n">level</span><span class="p">]</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">UnknowConfError</span><span class="p">(</span><span class="s1">&#39;exception occured&#39;</span><span class="p">)</span>
            <span class="n">num</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_dict</span></div>

    <span class="c1"># Check the key id format</span>
    <span class="k">def</span> <span class="nf">_check_key_valid</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>  <span class="c1"># pylint: disable=R0201</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        check if the key is valid</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="s1">&#39;&#39;</span> <span class="ow">or</span> <span class="n">key</span> <span class="o">==</span> <span class="s1">&#39;@&#39;</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">KeyFormatError</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">key</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;@&#39;</span><span class="p">:</span>
            <span class="n">key</span> <span class="o">=</span> <span class="n">key</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="k">for</span> <span class="n">char</span> <span class="ow">in</span> <span class="n">key</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">char</span><span class="o">.</span><span class="n">isalnum</span><span class="p">()</span> <span class="ow">and</span> <span class="n">char</span> <span class="o">!=</span> <span class="s1">&#39;_&#39;</span> \
                    <span class="ow">and</span> <span class="n">char</span> <span class="o">!=</span> <span class="s1">&#39;-&#39;</span> <span class="ow">and</span> <span class="n">char</span> <span class="o">!=</span> <span class="s1">&#39;.&#39;</span> <span class="ow">and</span> <span class="n">char</span> <span class="o">!=</span> <span class="s1">&#39;$&#39;</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">KeyFormatError</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>

    <span class="c1"># Check the [GROUP] key format</span>
    <span class="k">def</span> <span class="nf">_check_groupkey_valid</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;check if the group key is valid&quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">groupkey</span> <span class="ow">in</span> <span class="n">key</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_check_key_valid</span><span class="p">(</span><span class="n">groupkey</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_handle_include_syntx</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">line</span><span class="p">,</span> <span class="n">ignore_error</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;handle $include file.conf &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">ignore_error</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">include_file</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s1">&#39;&quot;&#39;</span><span class="p">)</span>
                <span class="n">include_dict</span> <span class="o">=</span> <span class="n">Configure2Dict</span><span class="p">(</span><span class="n">include_file</span><span class="p">)</span><span class="o">.</span><span class="n">get_dict</span><span class="p">(</span>
                    <span class="n">ignore_error</span>
                <span class="p">)</span>
                <span class="k">if</span> <span class="s1">&#39;$include&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_dict</span><span class="p">:</span>
                    <span class="n">newdict</span> <span class="o">=</span> <span class="n">ConfDict</span><span class="p">()</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_dict</span><span class="o">.</span><span class="n">set_ex</span><span class="p">(</span><span class="s1">&#39;$include&#39;</span><span class="p">,</span> <span class="n">newdict</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
                    <span class="n">newdict</span><span class="o">.</span><span class="n">set_ex</span><span class="p">(</span>
                        <span class="n">include_file</span><span class="p">,</span> <span class="n">include_dict</span><span class="p">,</span> <span class="s1">&#39;&#39;</span>
                    <span class="p">)</span>
            <span class="c1"># pylint: disable=W0703</span>
            <span class="c1"># Does not know exact exception type</span>
            <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
                <span class="n">cup</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                    <span class="s1">&#39;failed to handle include file, line:</span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                        <span class="n">line</span><span class="p">)</span>
                <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">include_file</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s1">&#39;&quot;&#39;</span><span class="p">)</span>
            <span class="n">include_dict</span> <span class="o">=</span> <span class="n">Configure2Dict</span><span class="p">(</span><span class="n">include_file</span><span class="p">)</span><span class="o">.</span><span class="n">get_dict</span><span class="p">(</span>
                <span class="n">ignore_error</span>
            <span class="p">)</span>
            <span class="k">if</span> <span class="s1">&#39;$include&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_dict</span><span class="p">:</span>
                <span class="n">newdict</span> <span class="o">=</span> <span class="n">ConfDict</span><span class="p">()</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_dict</span><span class="o">.</span><span class="n">set_ex</span><span class="p">(</span><span class="s1">&#39;$include&#39;</span><span class="p">,</span> <span class="n">newdict</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
                <span class="n">newdict</span><span class="o">.</span><span class="n">set_ex</span><span class="p">(</span>
                    <span class="n">include_file</span><span class="p">,</span> <span class="n">include_dict</span><span class="p">,</span> <span class="s1">&#39;&#39;</span>
                <span class="p">)</span>

    <span class="c1"># Read in the file content, with format check</span>
    <span class="c1"># pylint: disable=R0912,R0915</span>
    <span class="k">def</span> <span class="nf">_get_input_lines</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ignore_error</span><span class="p">,</span> <span class="n">encoding</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        read conf lines</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">fhandle</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">platforms</span><span class="o">.</span><span class="n">is_py2</span><span class="p">():</span>
                <span class="n">fhandle</span> <span class="o">=</span> <span class="n">_open_codecs</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_file</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="n">encoding</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">fhandle</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_file</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="n">encoding</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">IOError</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="n">cup</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s1">&#39;open file failed:</span><span class="si">%s</span><span class="s1">, err:</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_file</span><span class="p">,</span> <span class="n">error</span><span class="p">))</span>
            <span class="k">raise</span> <span class="ne">IOError</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">fhandle</span><span class="o">.</span><span class="n">readlines</span><span class="p">():</span>
            <span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
            <span class="c1"># if it&#39;s a blank line or a line with comments only</span>
            <span class="k">if</span> <span class="n">line</span> <span class="o">==</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
                <span class="n">line</span> <span class="o">=</span> <span class="s1">&#39;__comments__</span><span class="si">%s%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_separator</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;#&#39;</span><span class="p">):</span>
                <span class="n">line</span> <span class="o">=</span> <span class="s1">&#39;__comments__</span><span class="si">%s%s</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_separator</span><span class="p">,</span> <span class="n">line</span><span class="p">)</span>
                <span class="k">continue</span>
            <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;$include&#39;</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_handle_include_syntx</span><span class="p">(</span><span class="n">line</span><span class="p">,</span> <span class="n">ignore_error</span><span class="p">)</span>
                <span class="k">continue</span>
            <span class="c1"># if it&#39;s a section</span>
            <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;[&#39;</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;#&#39;</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="p">[:</span><span class="n">line</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;#&#39;</span><span class="p">)]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">line</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;]&#39;</span><span class="p">):</span>
                    <span class="k">raise</span> <span class="n">LineFormatError</span><span class="p">(</span><span class="s1">&#39;Parse line error, line:</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">+</span> <span class="n">line</span><span class="p">)</span>
                <span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
                <span class="n">key</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">lstrip</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_check_groupkey_valid</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>  <span class="c1"># check if key is valid</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_lines</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
                <span class="k">continue</span>
            <span class="c1"># key, value = line.split(&#39;:&#39;, 1)</span>
            <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_separator</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
            <span class="n">key</span> <span class="o">=</span> <span class="n">key</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
            <span class="n">value</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s1">&#39; </span><span class="se">\t</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="c1"># if remove_comments is True, delete comments in value.</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">_check_key_valid</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">value</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;&quot;&#39;</span><span class="p">):</span>     <span class="c1"># if the value is a string</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">value</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;&quot;&#39;</span><span class="p">):</span>
                    <span class="k">raise</span> <span class="n">ValueFormatError</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">key</span> <span class="o">!=</span> <span class="s1">&#39;__comments__&#39;</span><span class="p">:</span>
                    <span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_strip_value</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">value</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;&quot;&#39;</span><span class="p">):</span>
                <span class="n">tmp_value</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
                <span class="c1"># reserve escape in the value string</span>
                <span class="n">escape</span> <span class="o">=</span> <span class="kc">False</span>
                <span class="k">for</span> <span class="n">single</span> <span class="ow">in</span> <span class="n">value</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">escape</span><span class="p">:</span>
                        <span class="k">if</span> <span class="n">single</span> <span class="o">==</span> <span class="s1">&#39;0&#39;</span><span class="p">:</span>
                            <span class="n">tmp_value</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\0</span><span class="s1">&#39;</span>
                        <span class="k">elif</span> <span class="n">single</span> <span class="o">==</span> <span class="s1">&#39;n&#39;</span><span class="p">:</span>
                            <span class="n">tmp_value</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span>
                        <span class="k">elif</span> <span class="n">single</span> <span class="o">==</span> <span class="s1">&#39;r&#39;</span><span class="p">:</span>
                            <span class="n">tmp_value</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\r</span><span class="s1">&#39;</span>
                        <span class="k">elif</span> <span class="n">single</span> <span class="o">==</span> <span class="s1">&#39;t&#39;</span><span class="p">:</span>
                            <span class="n">tmp_value</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t</span><span class="s1">&#39;</span>
                        <span class="k">elif</span> <span class="n">single</span> <span class="o">==</span> <span class="s1">&#39;v&#39;</span><span class="p">:</span>
                            <span class="n">tmp_value</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\v</span><span class="s1">&#39;</span>
                        <span class="k">elif</span> <span class="n">single</span> <span class="o">==</span> <span class="s1">&#39;a&#39;</span><span class="p">:</span>
                            <span class="n">tmp_value</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\a</span><span class="s1">&#39;</span>
                        <span class="k">elif</span> <span class="n">single</span> <span class="o">==</span> <span class="s1">&#39;b&#39;</span><span class="p">:</span>
                            <span class="n">tmp_value</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\b</span><span class="s1">&#39;</span>
                        <span class="k">elif</span> <span class="n">single</span> <span class="o">==</span> <span class="s1">&#39;d&#39;</span><span class="p">:</span>
                            <span class="n">tmp_value</span> <span class="o">+=</span> <span class="sa">r</span><span class="s1">&#39;\d&#39;</span>
                        <span class="k">elif</span> <span class="n">single</span> <span class="o">==</span> <span class="s1">&#39;f&#39;</span><span class="p">:</span>
                            <span class="n">tmp_value</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\f</span><span class="s1">&#39;</span>
                        <span class="k">elif</span> <span class="n">single</span> <span class="o">==</span> <span class="s2">&quot;&#39;&quot;</span><span class="p">:</span>
                            <span class="n">tmp_value</span> <span class="o">+=</span> <span class="s2">&quot;&#39;&quot;</span>
                        <span class="k">elif</span> <span class="n">single</span> <span class="o">==</span> <span class="s1">&#39;&quot;&#39;</span><span class="p">:</span>
                            <span class="n">tmp_value</span> <span class="o">+=</span> <span class="s1">&#39;&quot;&#39;</span>
                        <span class="k">elif</span> <span class="n">single</span> <span class="o">==</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">:</span>
                            <span class="n">tmp_value</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span>
                        <span class="k">else</span><span class="p">:</span>
                            <span class="c1"># raise ValueFormatError(line)</span>
                            <span class="k">pass</span>
                        <span class="n">escape</span> <span class="o">=</span> <span class="kc">False</span>
                    <span class="k">elif</span> <span class="n">single</span> <span class="o">==</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">:</span>
                        <span class="n">escape</span> <span class="o">=</span> <span class="kc">True</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">tmp_value</span> <span class="o">+=</span> <span class="n">single</span>
                <span class="k">if</span> <span class="n">escape</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">ValueFormatError</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
                <span class="n">value</span> <span class="o">=</span> <span class="n">tmp_value</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_lines</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span>
        <span class="n">fhandle</span><span class="o">.</span><span class="n">close</span><span class="p">()</span></div>


<div class="viewcode-block" id="Dict2Configure"><a class="viewcode-back" href="../../../cup.util.html#cup.util.conf.Dict2Configure">[docs]</a><span class="k">class</span> <span class="nc">Dict2Configure</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Convert Dict into Configure.</span>
<span class="sd">    You can convert a ConfDict or python dict into a conf file.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1">##</span>
    <span class="c1"># @param dict the conf dict, make sure the type format is right</span>
    <span class="c1">#</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">conf_dict</span><span class="p">,</span> <span class="n">separator</span><span class="o">=</span><span class="s1">&#39;:&#39;</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_dict</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">set_dict</span><span class="p">(</span><span class="n">conf_dict</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_level</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_str</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_separator</span> <span class="o">=</span> <span class="n">separator</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_encoding</span> <span class="o">=</span> <span class="s1">&#39;utf8&#39;</span>

    <span class="k">def</span> <span class="nf">_get_field_value_sep</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_separator</span>

    <span class="c1"># The separator between each line</span>
    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_get_linesep</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span>

    <span class="c1"># The flag of an array</span>
    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_get_arrayflag</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;@&#39;</span>

    <span class="k">def</span> <span class="nf">_get_levelsep</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;.&#39;</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_level</span>

    <span class="k">def</span> <span class="nf">_get_arraylevel_sep</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;.&#39;</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_level</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_arrayflag</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_get_indents</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39; &#39;</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">_level</span> <span class="o">*</span> <span class="mi">4</span>

    <span class="k">def</span> <span class="nf">_get_write_string</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_str</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_level</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_get_confstring</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_dict</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_str</span>

<div class="viewcode-block" id="Dict2Configure.write_conf"><a class="viewcode-back" href="../../../cup.util.html#cup.util.conf.Dict2Configure.write_conf">[docs]</a>    <span class="k">def</span> <span class="nf">write_conf</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">conf_file</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="s1">&#39;utf8&#39;</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        write the conf into of the dict into a conf_file</span>

<span class="sd">        :param conf_file:</span>
<span class="sd">            the file which will be override</span>
<span class="sd">        :param encoding:</span>
<span class="sd">            &#39;utf8&#39; by default, specify yours if needed</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">fhandle</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="n">platforms</span><span class="o">.</span><span class="n">is_py2</span><span class="p">():</span>
            <span class="n">fhandle</span> <span class="o">=</span> <span class="n">_open_codecs</span><span class="p">(</span><span class="n">conf_file</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="n">encoding</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">fhandle</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">conf_file</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="n">encoding</span><span class="p">)</span>
        <span class="n">fhandle</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_write_string</span><span class="p">())</span>
        <span class="n">fhandle</span><span class="o">.</span><span class="n">close</span><span class="p">()</span></div>

    <span class="c1"># pylint: disable=R0911</span>
    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_comp_write_keys</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">valuex</span><span class="p">,</span> <span class="n">valuey</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">platforms</span><span class="o">.</span><span class="n">is_py2</span><span class="p">():</span>
            <span class="n">_py_type</span> <span class="o">=</span> <span class="p">[</span><span class="nb">bool</span><span class="p">,</span> <span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">,</span> <span class="nb">str</span><span class="p">,</span> <span class="n">unicode</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">_py_type</span> <span class="o">=</span> <span class="p">[</span><span class="nb">bool</span><span class="p">,</span> <span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">,</span> <span class="nb">str</span><span class="p">]</span>

        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">valuex</span><span class="p">)</span> <span class="o">==</span> <span class="nb">type</span><span class="p">(</span><span class="n">valuey</span><span class="p">):</span>
            <span class="k">return</span> <span class="mi">0</span>

        <span class="k">for</span> <span class="n">py_type</span> <span class="ow">in</span> <span class="n">_py_type</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">valuex</span><span class="p">,</span> <span class="n">py_type</span><span class="p">):</span>
                <span class="k">return</span> <span class="o">-</span><span class="mi">1</span>

        <span class="k">for</span> <span class="n">py_type</span> <span class="ow">in</span> <span class="n">_py_type</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">valuey</span><span class="p">,</span> <span class="n">py_type</span><span class="p">):</span>
                <span class="k">return</span> <span class="mi">1</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">valuex</span><span class="p">,</span> <span class="nb">list</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">valuey</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">valuex</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="nb">dict</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">valuex</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="nb">list</span><span class="p">):</span>
                    <span class="k">return</span> <span class="mi">1</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">return</span> <span class="o">-</span><span class="mi">1</span>
            <span class="c1"># pylint: disable=W0703</span>
            <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
                <span class="k">return</span> <span class="o">-</span><span class="mi">1</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="o">-</span><span class="mi">1</span>
        <span class="c1"># if isinstance(valuex, list) and isinstance(valuey, str):</span>
        <span class="c1">#     return 1</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">valuex</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
            <span class="k">return</span> <span class="mi">1</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">valuey</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
            <span class="k">return</span> <span class="o">-</span><span class="mi">1</span>
        <span class="k">return</span> <span class="mi">1</span>

    <span class="c1"># pylint: disable=R0912</span>
    <span class="k">def</span> <span class="nf">_get_confstring</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">_dict</span><span class="p">):</span>
        <span class="c1"># for item in sorted(</span>
        <span class="c1">#     _dict.items(), lambda x, y: self._comp_type(x[1], y[1])</span>
        <span class="c1"># ):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">order_keys</span> <span class="o">=</span> <span class="n">_dict</span><span class="o">.</span><span class="n">get_ordered_keys</span><span class="p">()</span>
        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
            <span class="n">order_keys</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">_dict</span><span class="o">.</span><span class="n">keys</span><span class="p">()))</span>
            <span class="n">order_keys</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span>
                <span class="n">key</span><span class="o">=</span><span class="n">functools</span><span class="o">.</span><span class="n">cmp_to_key</span><span class="p">(</span>
                    <span class="k">lambda</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_comp_write_keys</span><span class="p">(</span><span class="n">_dict</span><span class="p">[</span><span class="n">x</span><span class="p">],</span> <span class="n">_dict</span><span class="p">[</span><span class="n">y</span><span class="p">])</span>
                <span class="p">)</span>
            <span class="p">)</span>
        <span class="k">if</span> <span class="s1">&#39;$include&#39;</span> <span class="ow">in</span> <span class="n">order_keys</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">filepath</span> <span class="ow">in</span> <span class="n">_dict</span><span class="p">[</span><span class="s1">&#39;$include&#39;</span><span class="p">]:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_str</span> <span class="o">+=</span> <span class="s1">&#39;$include &quot;</span><span class="si">{0}</span><span class="s1">&quot;</span><span class="si">{1}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                    <span class="n">filepath</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_linesep</span><span class="p">()</span>
                <span class="p">)</span>
            <span class="n">order_keys</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="s1">&#39;$include&#39;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">order_keys</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="s1">&#39;$include&#39;</span><span class="p">:</span>
                <span class="n">cup</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">&#39;cup.conf does not support $include writeback yet&#39;</span><span class="p">)</span>
                <span class="k">continue</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">item</span> <span class="o">=</span> <span class="n">_dict</span><span class="o">.</span><span class="n">get_ex</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
                <span class="n">value</span> <span class="o">=</span> <span class="n">item</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                <span class="n">comments</span> <span class="o">=</span> <span class="n">item</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
                <span class="n">value</span> <span class="o">=</span> <span class="n">_dict</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
                <span class="n">comments</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">comment</span> <span class="ow">in</span> <span class="n">comments</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_str</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_indents</span><span class="p">()</span> <span class="o">+</span> <span class="n">comment</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">):</span>
                    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;its a tuple, key:</span><span class="si">{0}</span><span class="s1">, value:</span><span class="si">{1}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="nb">dict</span><span class="p">):</span>
                    <span class="c1"># items are all arrays</span>
                    <span class="c1"># [..@section]</span>
                    <span class="c1">#   abc:</span>
                    <span class="c1"># [..@section]</span>
                    <span class="c1">#   abc:</span>
                    <span class="k">for</span> <span class="n">ind</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">value</span><span class="p">)):</span>
                        <span class="k">try</span><span class="p">:</span>
                            <span class="n">item</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">get_ex</span><span class="p">(</span><span class="n">ind</span><span class="p">)</span>
                        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
                            <span class="n">item</span> <span class="o">=</span> <span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="n">ind</span><span class="p">],</span> <span class="p">[])</span>
                        <span class="k">for</span> <span class="n">comment</span> <span class="ow">in</span> <span class="n">item</span><span class="p">[</span><span class="mi">1</span><span class="p">]:</span>
                            <span class="bp">self</span><span class="o">.</span><span class="n">_str</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_indents</span><span class="p">()</span> <span class="o">+</span> <span class="n">comment</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">_add_arraylevel</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">_get_confstring</span><span class="p">(</span><span class="n">item</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">_minus_level</span><span class="p">()</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="c1"># a array list and array list has no sub-dict</span>
                    <span class="c1"># @item</span>
                    <span class="c1"># @item</span>
                    <span class="k">for</span> <span class="n">ind</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">value</span><span class="p">)):</span>
                        <span class="k">try</span><span class="p">:</span>
                            <span class="n">item</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">get_ex</span><span class="p">(</span><span class="n">ind</span><span class="p">)</span>
                        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
                            <span class="n">item</span> <span class="o">=</span> <span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="n">ind</span><span class="p">],</span> <span class="p">[])</span>
                        <span class="k">for</span> <span class="n">comment</span> <span class="ow">in</span> <span class="n">item</span><span class="p">[</span><span class="mi">1</span><span class="p">]:</span>
                            <span class="bp">self</span><span class="o">.</span><span class="n">_str</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_indents</span><span class="p">()</span> <span class="o">+</span> <span class="n">comment</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">_appendline</span><span class="p">(</span>
                            <span class="s1">&#39;</span><span class="si">{0}{1}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_arrayflag</span><span class="p">(),</span> <span class="n">key</span><span class="p">),</span>
                            <span class="n">item</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                        <span class="p">)</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_addlevel</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_get_confstring</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_minus_level</span><span class="p">()</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># type(value) == type(&quot;&quot;):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_appendline</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_get_confstring_ex</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">_dict</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">_appendline</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_str</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{0}{1}{2}{3}{4}{5}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_str</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_indents</span><span class="p">(),</span> <span class="n">key</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_field_value_sep</span><span class="p">(),</span>
            <span class="n">value</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_linesep</span><span class="p">()</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">_addlevel</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_str</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{0}{1}</span><span class="s1">[</span><span class="si">{2}{3}</span><span class="s1">]</span><span class="si">{4}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_str</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_indents</span><span class="p">(),</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_levelsep</span><span class="p">(),</span> <span class="n">key</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_get_linesep</span><span class="p">()</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_level</span> <span class="o">+=</span> <span class="mi">1</span>

    <span class="k">def</span> <span class="nf">_add_arraylevel</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_str</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{0}{1}</span><span class="s1">[</span><span class="si">{2}{3}</span><span class="s1">]</span><span class="si">{4}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_str</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_indents</span><span class="p">(),</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_arraylevel_sep</span><span class="p">(),</span>
            <span class="n">key</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_linesep</span><span class="p">()</span>
        <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_level</span> <span class="o">+=</span> <span class="mi">1</span>

    <span class="k">def</span> <span class="nf">_minus_level</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_level</span> <span class="o">-=</span> <span class="mi">1</span>

    <span class="c1"># Set the conf dict</span>
<div class="viewcode-block" id="Dict2Configure.set_dict"><a class="viewcode-back" href="../../../cup.util.html#cup.util.conf.Dict2Configure.set_dict">[docs]</a>    <span class="k">def</span> <span class="nf">set_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">conf_dict</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        set a new conf_dict</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">conf_dict</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;conf_dict is not a type of dict&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_dict</span> <span class="o">=</span> <span class="n">conf_dict</span></div>
        <span class="c1"># itemlist=sorted(dict.items(), lambda x,y: _comp_type(x[1],y[1]))</span>

    <span class="c1"># sort the dict, make type{dict} last</span>
    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_comp_type</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">item_a</span><span class="p">,</span> <span class="n">item_b</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">item_a</span><span class="p">)</span> <span class="ow">in</span> <span class="p">(</span><span class="nb">tuple</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">item_a</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">item_a</span> <span class="o">=</span> <span class="n">item_a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">item_b</span><span class="p">)</span> <span class="ow">in</span> <span class="p">(</span><span class="nb">tuple</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">item_b</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">item_b</span> <span class="o">=</span> <span class="n">item_b</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">item_a</span><span class="p">)</span> <span class="o">==</span> <span class="nb">type</span><span class="p">(</span><span class="n">item_b</span><span class="p">):</span>
            <span class="k">return</span> <span class="mi">0</span>
        <span class="k">elif</span> <span class="nb">type</span><span class="p">(</span><span class="n">item_b</span><span class="p">)</span> <span class="o">==</span> <span class="nb">dict</span><span class="p">:</span>
            <span class="k">return</span> <span class="o">-</span><span class="mi">1</span>
        <span class="k">elif</span> <span class="nb">type</span><span class="p">(</span><span class="n">item_a</span><span class="p">)</span> <span class="o">==</span> <span class="nb">dict</span><span class="p">:</span>
            <span class="k">return</span> <span class="mi">1</span>
        <span class="c1"># if type(item_a)!=type({}) or type(item_b)==type({}):</span>
        <span class="c1">#    return -1</span>
        <span class="k">return</span> <span class="mi">1</span></div>


<div class="viewcode-block" id="HdfsXmlConf"><a class="viewcode-back" href="../../../cup.util.html#cup.util.conf.HdfsXmlConf">[docs]</a><span class="k">class</span> <span class="nc">HdfsXmlConf</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    hdfs xmlconf modifier.</span>

<span class="sd">    Example:</span>
<span class="sd">    ::</span>

<span class="sd">        # modify and write new conf into hadoop-site.xmlconf</span>
<span class="sd">        xmlobj = xmlconf.HdfsXmlConf(xmlfile)</span>

<span class="sd">        # get hdfs conf items into a python dict</span>
<span class="sd">        key_values = xmlobj.get_items()</span>

<span class="sd">        # modify hdfs conf items</span>
<span class="sd">        for name in self._confdict[&#39;journalnode&#39;][&#39;hadoop_site&#39;]:</span>
<span class="sd">            if name in key_values:</span>
<span class="sd">                key_values[name][&#39;value&#39;] = \</span>
<span class="sd">                self._confdict[&#39;journalnode&#39;][&#39;hadoop_site&#39;][name]</span>
<span class="sd">        else:</span>
<span class="sd">            key_values[name] = {</span>
<span class="sd">                &#39;value&#39;: self._confdict[&#39;journalnode&#39;][&#39;hadoop_site&#39;][name],</span>
<span class="sd">                &#39;description&#39;: &#39; &#39;</span>
<span class="sd">            }</span>
<span class="sd">        hosts = &#39;,&#39;.join(self._confdict[&#39;journalnode&#39;][&#39;host&#39;])</span>
<span class="sd">        key_values[&#39;dfs.journalnode.hosts&#39;] = {</span>
<span class="sd">            &#39;value&#39;: hosts, &#39;description&#39;:&#39; journalnode hosts&#39;</span>
<span class="sd">        }</span>

<span class="sd">        # write back conf items with new values</span>
<span class="sd">        xmlobj.write_conf(key_values)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filepath</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">filepath</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">IOError</span><span class="p">(</span><span class="s1">&#39;file not found:</span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">filepath</span><span class="p">))</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">filepath</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">IOError</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{0}</span><span class="s1"> not a file&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">filepath</span><span class="p">))</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_xmlpath</span> <span class="o">=</span> <span class="n">filepath</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_confdict</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">_load_items</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_confdict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">dom</span> <span class="o">=</span> <span class="n">minidom</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_xmlpath</span><span class="p">)</span>
        <span class="n">properties</span> <span class="o">=</span> <span class="n">dom</span><span class="o">.</span><span class="n">getElementsByTagName</span><span class="p">(</span><span class="s1">&#39;property&#39;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">pro</span> <span class="ow">in</span> <span class="n">properties</span><span class="p">:</span>
            <span class="n">tmpdict</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">tmpdict</span><span class="p">[</span><span class="s1">&#39;value&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">pro</span><span class="o">.</span><span class="n">getElementsByTagName</span><span class="p">(</span>
                    <span class="s1">&#39;value&#39;</span>
                <span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">childNodes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">nodeValue</span>
            <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
                <span class="n">tmpdict</span><span class="p">[</span><span class="s1">&#39;value&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">tmpdict</span><span class="p">[</span><span class="s1">&#39;description&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">pro</span><span class="o">.</span><span class="n">getElementsByTagName</span><span class="p">(</span>
                    <span class="s1">&#39;description&#39;</span>
                <span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">childNodes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">nodeValue</span>
            <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
                <span class="n">tmpdict</span><span class="p">[</span><span class="s1">&#39;description&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_confdict</span><span class="p">[</span>
                <span class="n">pro</span><span class="o">.</span><span class="n">getElementsByTagName</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">childNodes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">nodeValue</span>
            <span class="p">]</span> <span class="o">=</span> <span class="n">tmpdict</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_confdict</span>

<div class="viewcode-block" id="HdfsXmlConf.get_items"><a class="viewcode-back" href="../../../cup.util.html#cup.util.conf.HdfsXmlConf.get_items">[docs]</a>    <span class="k">def</span> <span class="nf">get_items</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        return hadoop config items as a dict.</span>

<span class="sd">        :return:</span>
<span class="sd">            ::</span>

<span class="sd">                {</span>
<span class="sd">                    &#39;dfs.datanode.max.xcievers&#39;:  {</span>
<span class="sd">                        &#39;value&#39;: &#39;true&#39;, &#39;description&#39;: &#39;xxxxxxxxxx&#39;</span>
<span class="sd">                    },</span>
<span class="sd">                    ......</span>
<span class="sd">                }</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_load_items</span><span class="p">()</span></div>

    <span class="k">def</span> <span class="nf">_write_to_conf</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">new_confdict</span><span class="p">):</span>
        <span class="n">dom</span> <span class="o">=</span> <span class="n">minidom</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_xmlpath</span><span class="p">)</span>
        <span class="n">properties</span> <span class="o">=</span> <span class="n">dom</span><span class="o">.</span><span class="n">getElementsByTagName</span><span class="p">(</span><span class="s1">&#39;property&#39;</span><span class="p">)</span>
        <span class="n">tmpdict</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">new_confdict</span><span class="p">)</span>

        <span class="c1"># modify if name exists</span>
        <span class="k">for</span> <span class="n">pro</span> <span class="ow">in</span> <span class="n">properties</span><span class="p">:</span>
            <span class="n">name</span> <span class="o">=</span> <span class="n">pro</span><span class="o">.</span><span class="n">getElementsByTagName</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">childNodes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">nodeValue</span>
            <span class="n">valuenode</span> <span class="o">=</span> <span class="n">pro</span><span class="o">.</span><span class="n">getElementsByTagName</span><span class="p">(</span><span class="s1">&#39;value&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">tmpdict</span><span class="p">:</span>
                <span class="n">need_modify</span> <span class="o">=</span> <span class="kc">False</span>
                <span class="k">if</span> <span class="n">valuenode</span><span class="o">.</span><span class="n">firstChild</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">tmpdict</span><span class="p">[</span><span class="n">name</span><span class="p">][</span><span class="s1">&#39;value&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                        <span class="n">valuenode</span><span class="o">.</span><span class="n">appendChild</span><span class="p">(</span><span class="n">dom</span><span class="o">.</span><span class="n">createTextNode</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">))</span>
                        <span class="n">need_modify</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">need_modify</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="k">if</span> <span class="n">need_modify</span><span class="p">:</span>
                    <span class="n">valuenode</span><span class="o">.</span><span class="n">firstChild</span><span class="o">.</span><span class="n">replaceWholeText</span><span class="p">(</span>
                        <span class="n">tmpdict</span><span class="p">[</span><span class="n">name</span><span class="p">][</span><span class="s1">&#39;value&#39;</span><span class="p">]</span>
                    <span class="p">)</span>
                <span class="k">del</span> <span class="n">tmpdict</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">parent</span> <span class="o">=</span> <span class="n">pro</span><span class="o">.</span><span class="n">parentNode</span>
                <span class="n">parent</span><span class="o">.</span><span class="n">insertBefore</span><span class="p">(</span><span class="n">dom</span><span class="o">.</span><span class="n">createComment</span><span class="p">(</span><span class="n">pro</span><span class="o">.</span><span class="n">toxml</span><span class="p">()),</span> <span class="n">pro</span><span class="p">)</span>
                <span class="n">parent</span><span class="o">.</span><span class="n">removeChild</span><span class="p">(</span><span class="n">pro</span><span class="p">)</span>

        <span class="n">configuration_node</span> <span class="o">=</span> <span class="n">dom</span><span class="o">.</span><span class="n">getElementsByTagName</span><span class="p">(</span><span class="s1">&#39;configuration&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">tmpdict</span><span class="p">:</span>
            <span class="n">new_pro</span> <span class="o">=</span> <span class="n">dom</span><span class="o">.</span><span class="n">createElement</span><span class="p">(</span><span class="s1">&#39;property&#39;</span><span class="p">)</span>
            <span class="n">new_name</span> <span class="o">=</span> <span class="n">dom</span><span class="o">.</span><span class="n">createElement</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">)</span>
            <span class="n">new_name</span><span class="o">.</span><span class="n">appendChild</span><span class="p">(</span><span class="n">dom</span><span class="o">.</span><span class="n">createTextNode</span><span class="p">(</span><span class="n">name</span><span class="p">))</span>
            <span class="n">new_pro</span><span class="o">.</span><span class="n">appendChild</span><span class="p">(</span><span class="n">new_name</span><span class="p">)</span>
            <span class="c1"># value in the new property</span>
            <span class="n">new_value</span> <span class="o">=</span> <span class="n">dom</span><span class="o">.</span><span class="n">createElement</span><span class="p">(</span><span class="s1">&#39;value&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">new_value</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">new_value</span><span class="o">.</span><span class="n">appendChild</span><span class="p">(</span>
                    <span class="n">dom</span><span class="o">.</span><span class="n">createTextNode</span><span class="p">(</span><span class="n">tmpdict</span><span class="p">[</span><span class="n">name</span><span class="p">][</span><span class="s1">&#39;value&#39;</span><span class="p">])</span>
                <span class="p">)</span>
            <span class="n">new_pro</span><span class="o">.</span><span class="n">appendChild</span><span class="p">(</span><span class="n">new_value</span><span class="p">)</span>
            <span class="c1"># description</span>
            <span class="n">new_desc</span> <span class="o">=</span> <span class="n">dom</span><span class="o">.</span><span class="n">createElement</span><span class="p">(</span><span class="s1">&#39;description&#39;</span><span class="p">)</span>
            <span class="n">new_desc</span><span class="o">.</span><span class="n">appendChild</span><span class="p">(</span>
                <span class="n">dom</span><span class="o">.</span><span class="n">createTextNode</span><span class="p">(</span><span class="n">tmpdict</span><span class="p">[</span><span class="n">name</span><span class="p">][</span><span class="s1">&#39;description&#39;</span><span class="p">])</span>
            <span class="p">)</span>
            <span class="n">new_pro</span><span class="o">.</span><span class="n">appendChild</span><span class="p">(</span><span class="n">new_desc</span><span class="p">)</span>
            <span class="n">configuration_node</span><span class="o">.</span><span class="n">appendChild</span><span class="p">(</span><span class="n">new_pro</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">dom</span><span class="o">.</span><span class="n">toprettyxml</span><span class="p">(</span><span class="n">newl</span><span class="o">=</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>

<div class="viewcode-block" id="HdfsXmlConf.write_conf"><a class="viewcode-back" href="../../../cup.util.html#cup.util.conf.HdfsXmlConf.write_conf">[docs]</a>    <span class="k">def</span> <span class="nf">write_conf</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">kvs</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="s1">&#39;utf8&#39;</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        update config items with a dict kvs. Refer to the example above.</span>

<span class="sd">        :param kvs:</span>
<span class="sd">            ::</span>

<span class="sd">                {</span>
<span class="sd">                    key : { &#39;value&#39;: value, &#39;description&#39;: &#39;description&#39;},</span>
<span class="sd">                    ......</span>
<span class="sd">                }</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_encoding</span> <span class="o">=</span> <span class="n">encoding</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_load_items</span><span class="p">()</span>
        <span class="n">str_xml</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_write_to_conf</span><span class="p">(</span><span class="n">kvs</span><span class="p">)</span>
        <span class="n">fhandle</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="n">platforms</span><span class="o">.</span><span class="n">is_py2</span><span class="p">():</span>
            <span class="n">fhandle</span> <span class="o">=</span> <span class="n">_open_codecs</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_xmlpath</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="n">encoding</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">fhandle</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_xmlpath</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="n">encoding</span><span class="p">)</span>
        <span class="n">fhandle</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">str_xml</span><span class="p">)</span>
        <span class="n">fhandle</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_confdict</span> <span class="o">=</span> <span class="n">kvs</span></div></div>


<span class="k">def</span> <span class="nf">_main_hanle</span><span class="p">():</span>
    <span class="n">dict4afs</span> <span class="o">=</span> <span class="n">Configure2Dict</span><span class="p">(</span><span class="s1">&#39;/tmp/metaserver.conf&#39;</span><span class="p">)</span>
    <span class="n">dictafs</span> <span class="o">=</span> <span class="n">dict4afs</span><span class="o">.</span><span class="n">get_dict</span><span class="p">()</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">dictafs</span><span class="p">,</span> <span class="n">sort_keys</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>

<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;__main__&quot;</span><span class="p">:</span>
    <span class="k">pass</span>
    <span class="c1"># conf = CConf(g_prodUnitRuntime + &#39;Unitserver0/conf/&#39;,&#39;UnitServer.conf&#39;)</span>
    <span class="c1"># conf.update({&#39;MasterPort&#39;:&#39;1234&#39;,&#39;ProxyPortDelta&#39;:&#39;0&#39;})</span>
    <span class="c1"># conf.addAfterKeywordIfNoexist(</span>
    <span class="c1">#     &#39;SnapshotPatchLimit&#39;, (&#39;DelServerPerHourLimit&#39;, &#39;99&#39;)</span>
    <span class="c1"># )</span>

<span class="c1"># vi:set tw=0 ts=4 sw=4 nowrap fdm=indent</span>
</pre></div>

          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../../../index.html">cup</a></h1>








<h3>Navigation</h3>
<p class="caption" role="heading"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../cup.html">cup package</a></li>
</ul>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../../../index.html">Documentation overview</a><ul>
  <li><a href="../../index.html">Module code</a><ul>
  <li><a href="../util.html">cup.util</a><ul>
  </ul></li>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>document.getElementById('searchbox').style.display = "block"</script>








        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2023, CUP-DEV Team.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 7.0.1</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.13</a>
      
    </div>

    

    
  </body>
</html>